package com.bestom.t9.activity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.bestom.t9.R;
import com.bestom.t9mod.core.BaseActivity;
import com.zkteco.android.IDReader.IDPhotoHelper;
import com.zkteco.android.IDReader.WLTService;
import com.zkteco.android.biometric.core.device.ParameterHelper;
import com.zkteco.android.biometric.core.device.TransportType;
import com.zkteco.android.biometric.core.utils.LogHelper;
import com.zkteco.android.biometric.module.idcard.IDCardReader;
import com.zkteco.android.biometric.module.idcard.IDCardReaderFactory;
import com.zkteco.android.biometric.module.idcard.exception.IDCardReaderException;
import com.zkteco.android.biometric.module.idcard.meta.IDCardInfo;
import com.zkteco.android.biometric.module.idcard.meta.IDPRPCardInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


public class IDReaderActivity extends BaseActivity {
    private static final int VID = 1024;    //IDR VID
    private static final int PID = 50010;     //IDR PID
    private IDCardReader idCardReader = null;

    private TextView textView = null;
    private ImageView imageView = null;
    private boolean bopen = false;
    private boolean bStoped = false;
    private int mReadCount = 0;
    private CountDownLatch countdownLatch = null;

    private Context mContext = null;
    private Activity mActivity;
    private UsbManager musbManager = null;
    private final String ACTION_USB_PERMISSION = "com.example.scarx.idcardreader.USB_PERMISSION";

    private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        OpenDevice();
                    } else {
                        Toast.makeText(mContext, "USB未授权", Toast.LENGTH_SHORT).show();
                        Log.e("retType", "USB未授权");
                        //mTxtReport.setText("USB未授权");
                    }
                }
            }
        }
    };

    private void RequestDevicePermission() {
        musbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
        mContext.registerReceiver(mUsbReceiver, filter);

        for (UsbDevice device : musbManager.getDeviceList().values()) {

            if (device.getVendorId() == VID && device.getProductId() == PID) {
                Intent intent = new Intent(ACTION_USB_PERMISSION);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
                musbManager.requestPermission(device, pendingIntent);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idcard);
        textView = (TextView) findViewById(R.id.textView);
        imageView = (ImageView) findViewById(R.id.imageView);
        mContext = this.getApplicationContext();
        mActivity = this;

        //region init toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        //app logo
//        toolbar.setLogo(R.mipmap.ic_launcher_round);
        //title
        toolbar.setTitle("身份证读卡demo");
        //sub title
//        toolbar.setSubtitle("  Subtitle");
        //以上3个属性必须在setSupportActionBar(toolbar)之前调用
        setSupportActionBar(toolbar);
        //设置导航Icon，必须在setSupportActionBar(toolbar)之后设置
        toolbar.setNavigationIcon(R.mipmap.ic_back_green);
        //添加菜单点击事件
        toolbar.setNavigationOnClickListener(v -> {
            if (bopen) {
                OnBnStop(v);
            }
            mActivity.finish();
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //endregion

    }

    public Context getContext() {
        return this.getApplicationContext();
    }

    private void startIDCardReader() {
        // Define output log level
        LogHelper.setLevel(Log.VERBOSE);
        // Start fingerprint sensor
        Map idrparams = new HashMap();
        idrparams.put(ParameterHelper.PARAM_KEY_VID, VID);
        idrparams.put(ParameterHelper.PARAM_KEY_PID, PID);
        idCardReader = IDCardReaderFactory.createIDCardReader(this, TransportType.USB, idrparams);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Destroy fingerprint sensor when it's not used
        IDCardReaderFactory.destroy(idCardReader);
    }


    public static void writeLogToFile(String log) {
        try {
            File dirFile = new File("/sdcard/zkteco/");  //目录转化成文件夹
            if (!dirFile.exists()) {              //如果不存在，那就建立这个文件夹
                dirFile.mkdirs();
            }
            String path = "/sdcard/zkteco/idrlog.txt";
            File file = new File(path);
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file, true);
            log += "\r\n";
            outStream.write(log.getBytes());
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean authenticate() {
        try {
            idCardReader.findCard(0);
            idCardReader.selectCard(0);
            return true;
        } catch (IDCardReaderException e) {
            return false;
        }
    }

    private void exit(){
        if (idCardReader == null){
            onDestroy();
        }
    }

    public void OpenDevice() {
        if (bopen) {
            textView.setText("设备已连接");
            Log.d("retType", "设备已连接");
            return;
        }
        try {
            startIDCardReader();
            idCardReader.open(0);
            bStoped = false;
            mReadCount = 0;
            writeLogToFile("连接设备成功");
            textView.setText("连接成功");
            Log.d("retType", "连接成功");
            bopen = true;
            countdownLatch = new CountDownLatch(1);
            new Thread(() -> {
                while (!bStoped) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                    boolean ret = false;
                    final long nTickstart = System.currentTimeMillis();
                    try {
                        exit();
                        idCardReader.findCard(0);
                        idCardReader.selectCard(0);
                    } catch (IDCardReaderException e) {
                        //continue;
                    } catch (NullPointerException e){

                        Log.e("IDReaderActivity","捕捉到异常: 未停止读卡就退出");
                        e.printStackTrace();
                        return;
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int retType = 0;
                    try {
                        exit();
                        retType = idCardReader.readCardEx(0, 0);
                        Log.d("retType", "retType: " + retType);
                    } catch (IDCardReaderException e) {
                        Log.e("retType", "读卡失败，错误信息：" + e.getMessage());
                        writeLogToFile("读卡失败，错误信息：" + e.getMessage());
                    }
                    if (retType == 1 || retType == 2 || retType == 3) {
                        final long nTickUsed = (System.currentTimeMillis() - nTickstart);
                        final int final_retType = retType;
                        exit();
                        writeLogToFile("读卡成功：" + (++mReadCount) + "次" + "，耗时：" + nTickUsed + "毫秒");
                        runOnUiThread(() -> {
                            if (final_retType == 1) {

                                final IDCardInfo idCardInfo = idCardReader.getLastIDCardInfo();
                                //姓名adb
                                String strName = idCardInfo.getName();
                                //民族
                                String strNation = idCardInfo.getNation();
                                //出生日期
                                String strBorn = idCardInfo.getBirth();
                                //住址
                                String strAddr = idCardInfo.getAddress();
                                //身份证号
                                String strID = idCardInfo.getId();
                                //有效期限
                                String strEffext = idCardInfo.getValidityTime();
                                //签发机关
                                String strIssueAt = idCardInfo.getDepart();
                                textView.setText("读取次数：" + mReadCount + ",耗时：" + nTickUsed + "毫秒, 卡类型：居民身份证,姓名：" + strName +
                                        "，民族：" + strNation + "，住址：" + strAddr + ",身份证号：" + strID);
                                if (idCardInfo.getPhotolength() > 0) {
                                    byte[] buf = new byte[WLTService.imgLength];
                                    if (1 == WLTService.wlt2Bmp(idCardInfo.getPhoto(), buf)) {
                                        imageView.setImageBitmap(IDPhotoHelper.Bgr2Bitmap(buf));
                                    }
                                }
                            } else if (final_retType == 2) {
                                final IDPRPCardInfo idprpCardInfo = idCardReader.getLastPRPIDCardInfo();
                                //中文名
                                String strCnName = idprpCardInfo.getCnName();
                                //英文名
                                String strEnName = idprpCardInfo.getEnName();
                                //国家/国家地区代码
                                String strCountry = idprpCardInfo.getCountry() + "/" + idprpCardInfo.getCountryCode();//国家/国家地区代码
                                //出生日期
                                String strBorn = idprpCardInfo.getBirth();
                                //身份证号
                                String strID = idprpCardInfo.getId();
                                //有效期限
                                String strEffext = idprpCardInfo.getValidityTime();
                                //签发机关
                                String strIssueAt = "公安部";
                                textView.setText("读取次数：" + mReadCount + ",耗时：" + nTickUsed + "毫秒, 卡类型：外国人永居证,中文名：" + strCnName + ",英文名：" +
                                        strEnName + "，国家：" + strCountry + ",证件号：" + strID);
                                if (idprpCardInfo.getPhotolength() > 0) {
                                    byte[] buf = new byte[WLTService.imgLength];
                                    if (1 == WLTService.wlt2Bmp(idprpCardInfo.getPhoto(), buf)) {
                                        imageView.setImageBitmap(IDPhotoHelper.Bgr2Bitmap(buf));
                                    }
                                }
                            } else {
                                final IDCardInfo idCardInfo = idCardReader.getLastIDCardInfo();
                                //姓名
                                String strName = idCardInfo.getName();
                                //民族,港澳台不支持该项
                                String strNation = "";
                                //出生日期
                                String strBorn = idCardInfo.getBirth();
                                //住址
                                String strAddr = idCardInfo.getAddress();
                                //身份证号
                                String strID = idCardInfo.getId();
                                //有效期限
                                String strEffext = idCardInfo.getValidityTime();
                                //签发机关
                                String strIssueAt = idCardInfo.getDepart();
                                //通行证号
                                String strPassNum = idCardInfo.getPassNum();
                                //签证次数
                                int visaTimes = idCardInfo.getVisaTimes();
                                textView.setText("读取次数：" + mReadCount + ",耗时：" + nTickUsed + "毫秒, 卡类型：港澳台居住证,姓名：" + strName +
                                        "，住址：" + strAddr + ",身份证号：" + strID + "，通行证号码：" + strPassNum +
                                        ",签证次数：" + visaTimes);
                                if (idCardInfo.getPhotolength() > 0) {
                                    byte[] buf = new byte[WLTService.imgLength];
                                    if (1 == WLTService.wlt2Bmp(idCardInfo.getPhoto(), buf)) {
                                        imageView.setImageBitmap(IDPhotoHelper.Bgr2Bitmap(buf));
                                    }
                                }
                            }
                        });
                    }
                }
                countdownLatch.countDown();
            }).start();
        } catch (IDCardReaderException e) {
            writeLogToFile("连接设备失败");
            textView.setText("连接失败");
            textView.setText("开始读卡失败，错误码：" + e.getErrorCode() + "\n错误信息：" + e.getMessage() + "\n内部代码=" + e.getInternalErrorCode());
        }
    }

    public void OnBnBegin(View view) throws IDCardReaderException {

        if (bopen) {
            textView.setText("设备已连接");
            return;
        }
        RequestDevicePermission();

    }

    public void OnBnStop(View view) {
        if (!bopen) {
            return;
        }
        bStoped = true;
        mReadCount = 0;
        if (null != countdownLatch) {
            try {
                countdownLatch.await(2, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            idCardReader.close(0);
        } catch (IDCardReaderException e) {
            e.printStackTrace();
        }
        textView.setText("设备断开连接");
        bopen = false;

    }
}
